package com.ruoyi.lhb.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lhb.domain.Report;
import com.ruoyi.lhb.service.IReportService;
import com.ruoyi.lhb.util.Util;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.whc.domain.Nearman;
import com.ruoyi.whc.domain.Note;
import com.ruoyi.whc.domain.Positive;
import com.ruoyi.whc.service.INearmanService;
import com.ruoyi.whc.service.IPositiveService;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lhb.domain.Orderrecord;
import com.ruoyi.lhb.service.IOrderrecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 核酸预约列表Controller
 * 
 * @author lhb
 * @date 2022-09-22
 */
@RestController
@RequestMapping("/lhb/orderrecord")
public class OrderrecordController extends BaseController
{
    //密接人员添加
    @Autowired
    private INearmanService nearmanService;
    //隔离点列表接口
    @Autowired
    private IPositiveService positiveService;
    //核酸报告接口
    @Autowired
    private IReportService reportService;
    //后台用户接口
    @Autowired
    private ISysUserService userService;
    //预约列表接口
    @Autowired
    private IOrderrecordService orderrecordService;

    /**
     * 查询核酸预约列表列表
     */
    @PreAuthorize("@ss.hasPermi('lhb:orderrecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(Orderrecord orderrecord)
    {
        startPage();
        List<Orderrecord> list = orderrecordService.selectOrderrecordList(orderrecord);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出核酸预约列表列表
     */
    @PreAuthorize("@ss.hasPermi('lhb:orderrecord:export')")
    @Log(title = "核酸预约列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orderrecord orderrecord)
    {
        List<Orderrecord> list = orderrecordService.selectOrderrecordList(orderrecord);
        System.out.println(list);
        ExcelUtil<Orderrecord> util = new ExcelUtil<Orderrecord>(Orderrecord.class);
        util.exportExcel(response, list, "核酸预约列表数据");
    }

    /**
     * 获取核酸预约列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('lhb:orderrecord:query')")
    @GetMapping(value = "/{orid}")
    public AjaxResult getInfo(@PathVariable("orid") Long orid)
    {
        return AjaxResult.success(orderrecordService.selectOrderrecordByOrid(orid));
    }

    /**
     * 新增核酸预约列表
     */
    @PreAuthorize("@ss.hasPermi('lhb:orderrecord:add')")
    @Log(title = "核酸预约列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orderrecord orderrecord)
    {
        return toAjax(orderrecordService.insertOrderrecord(orderrecord));
    }

    /**
     * 新增用户核酸报告
     */
    @PreAuthorize("@ss.hasPermi('lhb:orderrecord:add')")
    @Log(title = "核酸预约列表", businessType = BusinessType.INSERT)
    @PutMapping
    public AjaxResult edit(@RequestBody Report report, SysUser sysUser, Orderrecord orderrecord, Positive positive, Nearman nearman)
    {

        sysUser.setUserId(Long.valueOf(SecurityUtils.getUserId()));
        List<SysUser> sysUserList = userService.selectUserList(sysUser);
        report.setUserid(SecurityUtils.getUserId());
        report.setTestinghospital(sysUserList.get(0).getDept().getDeptName());
        report.setReporttime(Util.getDate());
        report.setDetailedTime(Util.getTime());
        if (reportService.insertReport(report) == 1){
            orderrecord.setOrid(report.getOrid());
            orderrecord.setOstatus("1");
            orderrecordService.updateOrderrecord(orderrecord);
            if (report.getUstate().equals("阳性")){
                positive.setUid(Long.valueOf(report.getUid()));
                positive.setPstatus("0");
                positive.setPtime(Util.getDate());
                positiveService.insertPositive(positive);
                orderrecordService.updateUstate(report.getUid(),"102");
                Date d = new Date();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                System.out.println("今天的日期：" + df.format(d));
                System.out.println("两天前的日期：" + df.format(new Date(d.getTime() - 1 * 24 * 60 * 60 * 1000)));
                String twodate = df.format(new Date(d.getTime() - 1 * 24 * 60 * 60 * 1000));
                List<Note> noteList = orderrecordService.selectFrontnote(report.getUid(),twodate);
                for (int i = 0; i < noteList.size(); i++) {
                    String startime = (Integer.parseInt(noteList.get(i).getTime().split(":")[0])+1)+":"+noteList.get(i).getTime().split(":")[1];
                    String endtime = (Integer.parseInt(noteList.get(i).getTime().split(":")[0])-1)+":"+noteList.get(i).getTime().split(":")[1];
                    List<Note> closeNote = orderrecordService.selectUsFrontnote(noteList.get(i).getAddress(),noteList.get(i).getDate(),startime,endtime,report.getUid());
                    for (int j = 0; j < closeNote.size(); j++) {
                        System.out.println(noteList.get(i).getTrid() + "阳性人员行程id");
                        System.out.println(closeNote.get(j).getUid() + "密接人员id");
                        nearman.setUid(closeNote.get(j).getUid());
                        nearman.setTrid(noteList.get(i).getTrid());
                        nearmanService.insertNearman(nearman);
                        orderrecordService.updateUstate(String.valueOf(closeNote.get(j).getUid()),"101");
                    }
                }
            }
            return toAjax(1);
        }else {
            return toAjax(0);
        }
//        return toAjax(orderrecordService.updateOrderrecord(orderrecord));
    }

    /**
     * 删除核酸预约列表
     */
    @PreAuthorize("@ss.hasPermi('lhb:orderrecord:remove')")
    @Log(title = "核酸预约列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orids}")
    public AjaxResult remove(@PathVariable Long[] orids)
    {
        return toAjax(orderrecordService.deleteOrderrecordByOrids(orids));
    }
}
